import Vue from 'vue'
import VueRouter from "vue-router";
import LoginView from '../views/login/LoginView'
import HomeView from '../views/home/HomeView'
import Shopping from '../views/shopping/ShopPingView'
import detailsView from '../views/details/detailsView'
Vue.use(VueRouter)
const routes = [
    {
        path: '/',
        redirect:'/home',
    },
    {
        path:"/login",
        name:"login",
        component: LoginView
    },
    {
        path: '/menu',
        name: 'menu',
        component: () => import('../views/menuLis/MenuView'),
    },
    {
        path: '/home',
        name: 'home',
        component:HomeView,
    },
    {
        path:'/shop',
        name:'shop',
        component:Shopping,
    },
    {
        path:'/details',
        name:'details',
        component:detailsView,
    },
    {
        path: '/fskl',
        name: 'fskl',
        component: () => import('../views/giowe/FsKl.vue'),
    },
    {
        path: '/rtsv',
        name: 'rtsv',
        component: () => import('../views/giowe/RtSv.vue'),
    },
    {
        path: '/tmwl',
        name: 'tmwl',
        component: () => import('../views/giowe/TmWl.vue'),
    },
    {
        path: '/gehr',
        name: 'gehr',
        component: () => import('../views/giowe/GeHr.vue'),
    },
]


const router = new VueRouter({
    routes
})

// 导航守卫登录鉴权
router.beforeEach( (to,from,next)=>{
    if(localStorage.getItem('user')){
        next()
        console.log('已登录')
    }else{
        console.log('未登录')
        next()
    }
    // if(to.path=='/'||to.path=='/home'){
    //     // 验证本地存储中是否有user的token
    //     if(localStorage.getItem('user')){
    //         next()
    //         console.log('已登录')
    //     }else{
    //         console.log('未登录')
    //         next()
    //     }
    // }
} )
export default router;


//保存下来push方法 将来我们还会用到原push方法进行路由跳转
let originPush=VueRouter.prototype.push;
 
//接下来我们重写push|replace方法  
//第一个参数location参数像原push方法声明跳转的地方 resolve和reject传递成功与失败
 
VueRouter.prototype.push=function(location,resolve,reject){
   if(resolve && reject){
      //如果成功 调用原来的push方法  
           originPush.call(this,location,resolve,reject); 
               }else{
           originPush.call(this,location,()=>{},()=>{}); }    
  }
